import 'dart:convert';

import 'package:after_sale_plugin/package_event.dart';
import 'package:after_sale_plugin/pages/edit_order/cell/report_edit_item_cell.dart';
import 'package:after_sale_plugin/pages/problem_resolution/controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:wechat_assets_picker_plus/wechat_assets_picker_plus.dart';

class ProblemResolutionlView extends BaseGetView<ProblemResolutionController> {
  ProblemResolutionlView({super.key});

  @override
  String get title => AppLanguageUtil.to.titleProblemResolution;

  @override
  Color? get barBackgroundColor => Colors.black;

  @override
  Widget? get backView => CommonWidgets.appBarLeading();

  @override
  Widget buildBody(BuildContext context) {
    return Obx(
      () => Scaffold(
        backgroundColor: Colors.black,
        resizeToAvoidBottomInset: false,
        body: ListView(
          children: [
            ReportEditItem(
              ReportCellType.textView,
              InputType.text,
              AppLanguageUtil.to.titleProblemResolutionDesc,
              AppLanguageUtil.to.titleProblemResolutionDescPlacehodler,
              controller.state.resolutionContent,
              callbackTextFeildValue: (p0) {
                controller.state.allowedSubmit.value = p0.isNotEmpty;
                controller.state.resolutionContent = p0.obs;
              },
              bgHeight: 132,
              maxLength: 1000,
              space: CONTENT_MARGIN,
            ),
            Padding(
              padding: EdgeInsets.all(CONTENT_MARGIN),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  LxText(
                    AppLanguageUtil.to.titleProblemResolutionAssetDesc,
                    color: Colors.white.withOpacity(0.6),
                    fontWeightEnum: LxFontWeightEnum.bodyMedium,
                  ),
                  MediaContentEditView(
                    width: context.width - 2 * CONTENT_MARGIN,
                    assetModels: controller.state.assets.value,
                    addOnTap: () {
                      PackageEvent.selectAsssets(
                        (p0) {
                          // 添加资源文件
                          AssetModel assetModel =
                              AssetModel.fromMap(jsonDecode(p0));
                          bool isFull = controller.state.assets.length >= 9;
                          // 删除 ‘+’
                          if (isFull) {
                            controller.state.assets.removeLast();
                          }
                          controller.state.assets.insert(
                            controller.state.assets.length - (isFull ? 0 : 1),
                            assetModel,
                          );
                          // 上传资源文件
                          PackageEvent.uploadAsssets(
                            (fileName) {
                              AssetModel tempModel = assetModel
                                ..src =
                                    AssetModel.getUrl(OSS_FILE_URL, fileName);
                              for (var i = 0;
                                  i < controller.state.assets.length;
                                  i++) {
                                if (controller.state.assets[i].filePath ==
                                    tempModel.filePath) {
                                  controller.state.assets[i] = tempModel;
                                }
                              }
                            },
                            param: assetModel.filePath,
                          );
                        },
                        param: 9 - controller.state.assets.length + 1,
                      );
                    },
                    deleteOnTap: (model) {
                      controller.state.assets.remove(model);
                      if (!controller.state.assets.last.isAdd) {
                        controller.state.assets.add(AssetModel.add);
                      }
                    },
                    previewOnTap: (m) {},
                    itemBackground: const Color(0xFF141414),
                    isSupportDelete: true,
                  ),
                ],
              ),
            ),
            SizedBox(
              height: controller.state.assets.length >= 7 ? 50 : 150,
            ),
            LxButton(
              title: AppLanguageUtil.to.titleSubmitReport,
              onTap: controller.submit,
              buttonStausEnum: controller.state.allowedSubmit.isTrue ? ButtonStausEnum.normal
                  : ButtonStausEnum.disable,
            ),
          ],
        ),
      ),
    );
  }
}
